/**
 * 自定义函数
 * */


var formList = {
    data: [],
    pages: {start: 0, limit: 10}
}; //数据列表
var shopList = []; //商家列表
var formArys = {
    shopCode: '',
    img: '',
    total: 0
}; //表单数据


//列表
function getIndex(){
    var formData = {
        aid: getAid(),
        pageStart: formList.pages.start,
        pageLimit: formList.pages.limit
    };
    console.log(formData);
    $.ajax({
        type: apis.adShopIndex.method,
        url: domain + apis.adShopIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                if (formList.pages.start > 0) {
                    msgPop({msg: res.msg});
                }
                return
            }
            for (var i = 0; i < res.data.length; i ++) {
                formList.data.push(res.data[i]);
            }
            formList.pages.start ++;
            getHtml();
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//列表字符串
function getHtml(){
    var h = '', i;
    var d = formList.data;
    var phone, shopCode;
    h += '';
    for (i = 0; i < d.length; i ++) {
        phone = d[i].phone ? d[i].phone : '-';
        shopCode = d[i].shop_code ? d[i].shop_code : '-';
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>老板手机：' + phone + '<br/>门店：' + shopCode + '</td>';
        h += '<td><img src="' + d[i].link + '" height="50"/></td>';
        h += '<td>' + d[i].total + '</td>';
        h += '<td>' + d[i].num + '</td>';
        h += '<td>';
        h += d[i].isrun==2 ? '暂停' : '开启';
        h += ' &nbsp; ';
        if (d[i].isrun == 2) {
            h += '<a href="javascript:;" style="padding: 5px 10px; background: green; color: #fff;" onclick="setRun(' + d[i].id + ');">开启</a>';
            h += '<input type="hidden" id="status' + d[i].id + '" value="2"/>';
        } else {
            h += '<a href="javascript:;" style="padding: 5px 10px; background: red; color: #fff;" onclick="setRun(' + d[i].id + ');">暂停</a>';
            h += '<input type="hidden" id="status' + d[i].id + '" value="1"/>';
        }
        h += '</td>';
        h += '<td>';
        h += d[i].isdel==1 ? '使用中' : '<b style="color: red;">已删除</b>';
        if (d[i].isdel==1) {
            h += ' &nbsp; ';
            h += '<a href="javascript:;" style="padding: 5px 10px; background: red; color: #fff;" onclick="setDel(' + d[i].id + ');">删除</a>';
        }
        h += '</td>';
        h += '<td><a href="stat.html?id=' + d[i].id + '">' + (d[i].total * 1 - d[i].num) + '</a></td>';
        h += '<td>' + d[i].addtime + '</td>';
        h += '<td>' + d[i].uptime + '</td>';
        //h += '<td class="td-manage">';
        //h += '<a href="javascript:;" title="编辑" class="ml-5" onclick="viewEdit(' + d[i].id + ');">';
        //h += '编辑<i class="Hui-iconfont">&#xe6df;</i>';
        //h += '</a>';
        //h += '</td>';
        h += '</tr>';
    }
    h += '';
    $('#formList').html(h);
    $('#tableMore').show();
}


//商家选项
function getSelIndex(){
    var formData = {
        aid: getAid(),
        pageStart: formList.pages.start,
        pageLimit: formList.pages.limit
    };
    console.log(formData);
    $.ajax({
        type: apis.adShopSel.method,
        url: domain + apis.adShopSel.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                if (formList.pages.start > 0) {
                    msgPop({msg: res.msg});
                }
                return
            }
            shopList = res.data;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//表单新增
function viewAdd(){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var h = '';
    h += '<style type="text/css">select{padding: 5px 0; width: 100%; border: 1px solid #eee;}</style>';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.6) + 'px; text-align: center; background: #fff; color: #333; border-radius: 5px; position: fixed; top: 5%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">';
    h += '<div style="text-align: center; font-weight: bolder; font-size: 20px;">新增广告</div>';
    h += '<form class="form form-horizontal" style="padding: 20px; max-height: 500px; overflow-x: hidden; overflow-y: auto;">';
    //商家选择
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>门店选择：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<select onchange="formArys.shopCode=this.value;">';
    h += '<option value="">--选择--</option>';
    for (var i = 0; i < shopList.length; i ++) {
        var sname = shopList[i].phone + '-' + shopList[i].shopName;
        h += '<option value="' + shopList[i].shopCode + '">' + sname + '</option>';
    }
    h += '</select>';
    h += '</div>';
    h += '</div>';
    //图片
    h += '<style type="text/css"></style>';
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>广告图片：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<div style="padding: 5px 0; width: 100px; background: #0088cc; text-align: center; font-size: 12px; color: #fff; border-radius: 5px; position: relative;">重新上传图片</div>';
    h += '<input type="file" id="uploadFile" style="width: 180px; height: 30px; opacity: 0.1; cursor: pointer; position: relative; top: -30px; left: -80px;" onchange="uploadImage()"/>';
    h += '<div id="img" style="margin-top: -20px; width: 400px; height: 100px; background: #eee; overflow: hidden;"></div>';
    h += '</div>';
    h += '</div>';
    //要展示次数
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>展示次数：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<input type="text" class="input-text" placeholder="输入次数" value="5" onblur="formArys.total=this.value;"/>';
    formArys.total = 5;
    h += '</div>';
    h += '</div>';
    //按钮
    h += '<div class="row cl">';
    //h += '<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">';
    h += '<div class="col-xs-8 col-sm-9 col-xs-offset-3 col-sm-offset-2">';
    h += '<button class="btn btn-default radius" type="button" onClick="$(\'#formPop\').html(\'\');">&nbsp; 取 消 &nbsp;</button> <button class="btn btn-secondary radius" type="button" onClick="adAdd();"><i class="Hui-iconfont">&#xe632;</i> 保 存 </button>';
    h += '</div>';
    h += '</div>';
    h += '</form>';
    h += '</div>';
    h += '</div>';
    h += '';
    $('#formPop').html(h);
}


//显示图片
function showImg(e){
    var h = '';
    h += '<img src="' + e + '" style="width: 400px; height: 100px;"/>';
    h += '<input type="hidden" id="imgLink" value="' + e + '"/>';
    //h += '<a href="javascript:;" style="padding: 5px 10px; color: #fff; background: red; font-size: 12px; border: 0; border-radius: 0; opacity: 0.7; position: relative; top: -110px; left: 0;" onclick="imgDel();">点击删除</a>';
    $('#img').html(h);
    formArys.img = e;
}


//广告新增
function adAdd(){
    if (formArys.shopCode == '') {
        msgPop({msg: '门店必须的'}); return
    }
    if (formArys.img == '') {
        msgPop({msg: '图片必须的'}); return
    }
    if (formArys.total <= 0) {
        msgPop({msg: '展示次数必须的'}); return
    }
    var formData = {
        aid: getAid(),
        shopCode: formArys.shopCode,
        img: formArys.img,
        total: formArys.total
    };
    console.log(formData);
    $.ajax({
        type: apis.adShopAdd.method,
        url: domain + apis.adShopAdd.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '添加成功'});
            window.location.href = '';
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//开启|暂停
function setRun(id){
    var formData = {
        aid: getAid(),
        adid: id
    };
    console.log(formData);
    $.ajax({
        type: apis.adShopRun.method,
        url: domain + apis.adShopRun.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '修改成功'});
            window.location.href = '';
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function setDel(id){
    var formData = {
        aid: getAid(),
        adid: id
    };
    console.log(formData);
    $.ajax({
        type: apis.adShopDel.method,
        url: domain + apis.adShopDel.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '删除成功'});
            window.location.href = '';
        },
        error: function () {
            console.log('网络错误');
        }
    });
}